package cn.edu.zafu.bigdata.x.zyy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;

class Student {
    private String id;
    private String name;
    private String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

class DatabaseConnection {
    private static final String URL = "jdbc:mysql://192.168.169.76:3306/learnjava";
    private static final String USER = "learn";
    private static final String PASSWORD = "learnpassword";

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }
}

class StudentManager {
    public void addStudent(Student student) {
        String sql = "INSERT INTO students (id, name, phone) VALUES (?, ?, ?)";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, student.getId());
            pstmt.setString(2, student.getName());
            pstmt.setString(3, student.getPhone());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Student findStudentById(String id) {
        String sql = "SELECT * FROM students WHERE id = ?";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return new Student(rs.getString("id"), rs.getString("name"), rs.getString("phone"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void updateStudent(Student student) {
        String sql = "UPDATE students SET name = ?, phone = ? WHERE id = ?";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, student.getName());
            pstmt.setString(2, student.getPhone());
            pstmt.setString(3, student.getId());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteStudent(String id) {
        String sql = "DELETE FROM students WHERE id = ?";
        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}


public class stMain {
    private static final Scanner scanner = new Scanner(System.in);
    private static StudentManager studentManager = new StudentManager();

    public static void main(String[] args) {
        boolean running = true;
        while (running) {
            System.out.println("学生信息管理系统");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            String input = scanner.nextLine();
            switch (input) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    findStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    deleteStudent();
                    break;
                case "5":
                    running = false;
                    break;
                default:
                    System.out.println("无效的输入，请重新输入。");
                    break;
            }
        }
        scanner.close();
    }

    private static void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        while (true) {
            Student student = studentManager.findStudentById(id);
            if (student != null) {
                System.out.println("学号重复，请重新输入学号: ");
                id = scanner.nextLine();
            } else {
                break;
            }
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        Student newStudent = new Student(id, name, phone);
        studentManager.addStudent(newStudent);
        System.out.println("学生信息添加成功。");
    }

    private static void findStudent() {
        System.out.print("请输入学号或输入'QUIT'退出: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            Student student = studentManager.findStudentById(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("请输入学号或输入'QUIT'退出: ");
            id = scanner.nextLine();
        }
    }

    private static void updateStudent() {
        System.out.print("请输入学号或输入'QUIT'退出: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            Student student = studentManager.findStudentById(id);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                studentManager.updateStudent(student);
                System.out.println("学生信息更新成功。");
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("请输入学号或输入'QUIT'退出: ");
            id = scanner.nextLine();
        }
    }

    private static void deleteStudent() {
        System.out.print("请输入学号或输入'QUIT'退出: ");
        String id = scanner.nextLine();
        while (!id.equalsIgnoreCase("QUIT")) {
            Student student = studentManager.findStudentById(id);
            if (student != null) {
                studentManager.deleteStudent(id);
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生。");
            }
            System.out.print("请输入学号或输入'QUIT'退出: ");
            id = scanner.nextLine();
        }
    }
}